<?php

namespace App\Http\Controllers\Api;

use App\Models\Article;
use App\Repositories\ArticleRepository;
use App\Scopes\DraftScope;
use App\Transformers\ArticleTransformer;
use Illuminate\Http\Request;
use App\Http\Requests\ArticleRequest;

class ArticleController extends ApiController
{


    /**
     * 获取文章列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $articles = Article::applyFilter($request)
            ->withSimpleSearch()
            ->withSort()
            ->paginate($this->perPage());
        return $this->response()->paginator($articles, new ArticleTransformer())->setMeta(Article::getAllowSearchFieldsMeta() + Article::getAllowSortFieldsMeta());
    }

    public function store(ArticleRequest $request, ArticleRepository $articleRepository)
    {
        $articleRepository->create($request->validated());
        return $this->response()->noContent();
    }

    public function update(Article $article, ArticleRequest $request, ArticleRepository $articleRepository)
    {
        $articleRepository->update($request->validated(), $article);
        return $this->response()->noContent();
    }

    public function edit($id)
    {
//        return $this->response()->item($article, new ArticleTransformer());
        $article = Article::withoutGlobalScope(DraftScope::class)->findOrFail($id);
//
        return $this->response()->item($article, new ArticleTransformer());
    }

    /**
     * @param Article $article
     * @return mixed
     * @throws \Exception
     */
    public function destroy(Article $article)
    {
        $article->delete();
        return $this->response()->noContent();
    }

//    /**
//     * 真删除
//     * @param $article
//     * @return \App\Support\Response\Response
//     */
//    public function realDestroy($article)
//    {
//        $articleModel = app(Article::class);
//        $articleModel->where($articleModel->getRouteKeyName(), $article)->forceDelete();
//        $articleModel->articleContent()->forceDelete();
//        return $this->response()->noContent();
//    }
//
//
//    /**
//     * 恢复软删除
//     * @param $article
//     * @return mixed
//     */
//    public function restore($article)
//    {
//        $articleModel = app(Article::class);
//        $articleModel->onlyTrashed()->where($articleModel->getRouteKeyName(), $article)->restore();
//        return $this->response()->noContent();
//    }


//    /**
//     * Display a listing of the resource.
//     *
//     * @return \Illuminate\Http\JsonResponse
//     */
//    public function index(Request $request)
//    {
//        $keyword = $request->get('keyword');
//
//        $articles = Article::checkAuth()
//            ->when($keyword, function ($query) use ($keyword) {
//                $query->where('title', 'like', "%{$keyword}%")
//                    ->orWhere('subtitle', 'like', "%{$keyword}%");
//            })
//            ->orderBy('created_at', 'desc')->paginate(10);
//
//        return $this->response->collection($articles);
//    }
//
//    /**
//     * Store a newly created resource in storage.
//     *
//     * @param \App\Http\Requests\ArticleRequest $request
//     *
//     * @return \Illuminate\Http\JsonResponse
//     */
//    public function store(ArticleRequest $request)
//    {
//        $data = array_merge($request->all(), [
//            'user_id' => \Auth::id(),
//            'last_user_id' => \Auth::id(),
//        ]);
//
//        $data['is_draft'] = isset($data['is_draft']);
//        $data['is_original'] = isset($data['is_original']);
//
//        if(!$data['cover']){
//            $fileName = mt_rand(1, 299);
//            $data['cover'] = 'http://pi88z466o.bkt.clouddn.com/article/random/'.$fileName.'.jpg';
//        }
//
//        if(!$data['published_at']){
//            $data['published_at'] = date('Y-m-d H:i:s', time());
//        }
//
//        $article = new Article();
//        $article->fill($data);
//        $article->save();
//        if($request->get('tags')){
//            $article->tags()->sync(json_decode($request->get('tags')));
//        }
//        $article->articleContent()->sync($request->get('content'));
//
//        return $this->response->noContent();
//    }
//
//    /**
//     * Show the form for editing the specified resource.
//     *
//     * @param int $id
//     *
//     * @return \Illuminate\Http\JsonResponse
//     */
//    public function edit($id)
//    {
//        $article = Article::withoutGlobalScope(DraftScope::class)->findOrFail($id);
//
//        return $this->response->item($article);
//    }
//
//    /**
//     * Update the specified resource in storage.
//     *
//     * @param \App\Http\Requests\ArticleRequest $request
//     * @param int                               $id
//     *
//     * @return \Illuminate\Http\JsonResponse
//     */
//    public function update(ArticleRequest $request, $id)
//    {
//        $data = array_merge($request->all(), [
//            'last_user_id' => \Auth::id(),
//        ]);
//        $article = Article::withoutGlobalScope(DraftScope::class)->findOrFail($id);
//        $article->update($data);
//
//        if($request->get('tags')){
//            $article->tags()->sync(json_decode($request->get('tags')));
//        }
//
//
//        $article->articleContent()->sync($request->get('content'));
//
//        return $this->response->noContent();
//    }
//
//    /**
//     * Remove the specified resource from storage.
//     *
//     * @param int $id
//     *
//     * @return \Illuminate\Http\JsonResponse
//     */
//    public function destroy($id)
//    {
//        Article::withoutGlobalScope(DraftScope::class)->findOrFail($id)->delete();
//
//        return $this->response->noContent();
//    }
}
